package com.example.blog.controller;

import com.yc.common.vo.Result;
import com.yc.upload.web.UploadController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UeditorAction {

    @RequestMapping(path = "js/ueditor-1.4.3.3/ctrl",params="action=config")
    public Map<String,String> ctrl(){
        Map<String,String> res = new HashMap<>();
        res.put("imageActionName","upload");
        res.put("imageFieldName","file");
        res.put("imageUrlPrefix","");
        return res;
    }

    @Resource
    UploadController uploadController;


    /**
     *     "state": "SUCCESS",
     *     "url": "upload/demo.jpg",
     *     "title": "demo.jpg",
     *     "original": "demo.jpg"
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "js/ueditor-1.4.3.3/ctrl",params = "action=upload")
    public Map<String,String> ctrl(@RequestParam("file")MultipartFile file) throws IOException {
        Result res = uploadController.upload(file, "img");
        Map<String,String> map = new HashMap<>();
        map.put("state", "SUCCESS");
        map.put("url", res.getData().toString());
        map.put("title", "image.jpg");
        map.put("original", "image.jpg");
        return map;
    }
}
